﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Moved;

namespace WebChamCong
{
    public partial class Menu : System.Web.UI.UserControl
    {
        private List<DTO.BoPhanDTO> _boPhans;

        public  List<DTO.BoPhanDTO> BoPhans
        {
            get
            {
                return this._boPhans;
            }
            set
            {
                this._boPhans = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                LoadMenu();
            }
        }

        private  void LoadMenu()
        {
            try
            {
                string localstr = Session["departmentID"].ToString();
                string[] str = localstr.Split('&');
                if (str.Length == 1)
                {
                    DTO.RoleDTO dd = BUS.RolesBUS.getRoleByID(int.Parse(str[0]));
                    if (dd.UserRoles == null)
                    {
                        // Get departments into database
                        List<DTO.BoPhanDTO> localBoPhans = BUS.BoPhanBUS.GetBoPhans();

                        this._boPhans = localBoPhans;

                        // Binding to listmenu components
                        String result = "";

                        foreach (DTO.BoPhanDTO item in localBoPhans)
                        {
                            result += @"<li><a href='Index.aspx?id=" + item.DepartmentID + "'>";
                            result += @"" + item.DepartmentName + " (" + BUS.NhanVienBUS.CountNhanVienByDepartID(item.DepartmentID) + ") </a></li>";
                        }

                        this.listMenu.InnerHtml = result;
                    }
                    else
                    {
                        if (dd.UserRoles == "-1")
                        {
                            this.listMenu.Visible = false;
                            //DTO.NhanVienDTO dt = BUS.NhanVienBUS.getUserIDByName(Session["username"].ToString());
                            //String result = "";
                            //result += @"<li><a href='Index.aspx?id=" + dt.DepartmentId + "'>";
                            //result += @"" + dt.DepartmentName + " (" + BUS.NhanVienBUS.CountNhanVienByDepartID(dt.DepartmentId) + ") </a></li>";
                            //this.listMenu.InnerHtml = result;
                        }
                        else
                        {
                            DTO.BoPhanDTO bp = BUS.BoPhanBUS.GetBoPhanById(Convert.ToInt32(dd.UserRoles));
                            String result = "";
                            result += @"<li><a href='Index.aspx?id=" + bp.DepartmentID + "'>";
                            result += @"" + bp.DepartmentName + " (" + BUS.NhanVienBUS.CountNhanVienByDepartID(bp.DepartmentID) + ") </a></li>";
                            this.listMenu.InnerHtml = result;
                        }
                    }
                }
                else
                {
                    String result = "";
                    for (int i = 0; i < str.Count(); i++)
                    {
                        if (str[i] != "")
                        {
                            DTO.RoleDTO dd = BUS.RolesBUS.getRoleByID(int.Parse(str[i]));
                            DTO.BoPhanDTO bp = BUS.BoPhanBUS.GetBoPhanById(Convert.ToInt32(dd.UserRoles));
                            result += @"<li><a href='Index.aspx?id=" + bp.DepartmentID + "'>";
                            result += @"" + bp.DepartmentName + " (" + BUS.NhanVienBUS.CountNhanVienByDepartID(bp.DepartmentID) + ") </a></li>";
                        }
                    }
                    this.listMenu.InnerHtml = result;
                }

            }            
            catch (Exception ex)
            {
                this.listMenu.InnerHtml = "Failed : "+ex.Message;              
            }           
        }
    }
}